package com.example.vaultmysql.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 数据库配置实体类
 * 
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "database_config")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DatabaseConfig {

    @Id
    @Column(length = 50)
    private String id;

    @Column(name = "name", length = 100, nullable = false, unique = true)
    private String name;

    @Column(name = "host", length = 255, nullable = false)
    private String host;

    @Column(name = "port", nullable = false)
    private Integer port = 3306;

    @Column(name = "schema_name", length = 100, nullable = false)
    private String schemaName;

    @Column(name = "vault_path", length = 255, nullable = false)
    private String vaultPath;

    @Column(name = "enabled", nullable = false)
    private Boolean enabled = true;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    /**
     * 构造函数
     */
    public DatabaseConfig(String id, String name, String host, Integer port, 
                         String schemaName, String vaultPath) {
        this.id = id;
        this.name = name;
        this.host = host;
        this.port = port;
        this.schemaName = schemaName;
        this.vaultPath = vaultPath;
        this.enabled = true;
    }

    /**
     * 获取JDBC URL
     */
    public String getJdbcUrl() {
        return String.format("jdbc:mysql://%s:%d/%s?useSSL=true&serverTimezone=UTC&allowPublicKeyRetrieval=true", 
                           host, port, schemaName);
    }

    /**
     * 获取显示名称
     */
    public String getDisplayName() {
        return String.format("%s (%s:%d)", name, host, port);
    }
}